package com.cardone.platform.configuration.service;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.service.PermissionService;
import com.cardone.platform.configuration.dao.SiteDao;
import com.cardone.platform.configuration.dto.SiteDto;

/**
 * 站服务
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class SiteDefaultService implements SiteService {
	@Override
	public int deleteByIds(final SiteDto deleteSite) {
		return ContextHolder.getBean(SiteDao.class).deleteByIds(deleteSite);
	}

	@Override
	public <P> P findByCode(final Class<P> mappedClass, final String code) {
		return ContextHolder.getBean(SiteDao.class).findByCode(mappedClass, code);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final SiteDto findSite) {
		return ContextHolder.getBean(SiteDao.class).findByCodeOrId(mappedClass, findSite);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final SiteDto findSite) {
		return ContextHolder.getBean(SiteDao.class).findById(mappedClass, findSite);
	}

	@Override
	public <P> P findByServerName(final Class<P> mappedClass, final String serverName, final String projectCode, final String code) {
		final P p = ContextHolder.getBean(SiteDao.class).findByServerName(mappedClass, serverName, projectCode, code);

		if (p instanceof SiteDto) {
			final SiteDto site = (SiteDto) p;

			final List<String> urlWhiteList = ContextHolder.getBean(PermissionService.class).readCodeListBySiteIdAndTypeCode(site.getId(), "urlWhiteList");

			site.setUrlWhiteList(urlWhiteList);

			final List<String> urlBlackList = ContextHolder.getBean(PermissionService.class).readCodeListBySiteIdAndTypeCode(site.getId(), "urlBlackList");

			site.setUrlBlackList(urlBlackList);
		}

		return p;
	}

	@Override
	public <P> P findBySiteUrlCode(final Class<P> mappedClass, final String siteUrlCode) {
		return ContextHolder.getBean(SiteDao.class).findBySiteUrlCode(mappedClass, siteUrlCode);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final SiteDto findListSite) {
		return ContextHolder.getBean(SiteDao.class).findListByLikeCode(mappedClass, findListSite);
	}

	@Override
	public <P> List<P> findListByProjectCode(final Class<P> mappedClass, final String projectCode) {
		return ContextHolder.getBean(SiteDao.class).findListByProjectCode(mappedClass, projectCode);
	}

	@Override
	public void init(final List<SiteDto> initSiteList) {
		if (CollectionUtils.isEmpty(initSiteList)) {
			return;
		}

		for (final SiteDto initSite : initSiteList) {
			final String projectId = ContextHolder.getBean(DictionaryService.class).readIdByTypeCodeAndCode(initSite.getProjectTypeCode(), initSite.getProjectCode());

			if (StringUtils.isBlank(projectId)) {
				continue;
			}

			initSite.setProjectId(projectId);

			final String styleId = ContextHolder.getBean(DictionaryService.class).readIdByTypeCodeAndCode(initSite.getStyleTypeCode(), initSite.getStyleCode());

			if (StringUtils.isBlank(styleId)) {
				continue;
			}

			initSite.setStyleId(styleId);

			ContextHolder.getBean(SiteDao.class).saveByIdOrCode(SiteDto.class, initSite);
		}
	}

	@Override
	public int insertByCode(final SiteDto insertSite) {
		return ContextHolder.getBean(SiteDao.class).insertByCode(insertSite);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final SiteDto paginationSite) {
		return ContextHolder.getBean(SiteDao.class).paginationByLikeCode(mappedClass, paginationSite);
	}

	@Override
	public Integer readByCodeNqIdForCount(final SiteDto readSite) {
		return ContextHolder.getBean(SiteDao.class).readByCodeNqIdForCount(readSite);
	}

	@Override
	public String readIdByCode(final String code) {
		return ContextHolder.getBean(SiteDao.class).readIdByCode(code);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final SiteDto saveSite) {
		return ContextHolder.getBean(SiteDao.class).saveByIdOrCode(mappedClass, saveSite);
	}

	@Override
	public int updateByCode(final SiteDto updateSite) {
		return ContextHolder.getBean(SiteDao.class).updateByCode(updateSite);
	}
}